package com.nine.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nine.shop.common.ServerResponse;
import com.nine.shop.mapper.UserMapper;
import com.nine.shop.model.User;
import com.nine.shop.utils.MD5Util;
import org.springframework.stereotype.Service;


/**
 * @author chenyi
 * Create on 2019/8/17
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    /**
     * 查找用户名
     *
     * @param userName
     * @return
     */
    public User findByUserName(String userName) {
        return baseMapper.selectOne(new QueryWrapper<User>()
                .eq("userName", userName));
    }

    /**
     * 密码隐藏
     *
     * @param user
     * @return
     */
    public User changeUser(User user) {
        User newuser = new User();
        newuser.setId(user.getId());
        newuser.setUserName(user.getUserName());
        newuser.setEmail(user.getEmail());
        newuser.setStatus(user.getStatus());
        newuser.setBalance(user.getBalance());
        return newuser;
    }

    /**
     * 登录
     *
     * @param loginuser
     * @return
     */
    public ServerResponse login(User loginuser) {
        String password = MD5Util.MD5EncodeUtf8(loginuser.getPassword());
        User user = findByUserName(loginuser.getUserName());
        if (user == null)
            return ServerResponse.createByErrorMsg("用户不存在");
        if (!user.getPassword().equals(password))
            return ServerResponse.createByErrorMsg("密码错误");
        if (user.getStatus() == 0) {
            return ServerResponse.createByErrorMsg("用户未激活");
        }
        User user1 = changeUser(user);//隐藏密码
        return ServerResponse.createBySuccess(user1);//登录成功
    }

    /**
     * 注册1
     *
     * @param user
     * @return
     */
    public ServerResponse register(User user) {
        User u = findByUserName(user.getUserName());
        if (user.getUserName() == null || user.getUserName().equals("")) {
            return ServerResponse.createByErrorMsg("用户名为空");
        } else if (user.getPassword() == null || user.getPassword().equals("")) {
            return ServerResponse.createByErrorMsg("密码为空");
        } else if (user.getEmail() == null || user.getEmail().equals("")) {
            return ServerResponse.createByErrorMsg("邮箱为空");
        } else if (u != null) {
            return ServerResponse.createByErrorMsg("用户名已存在");
        } else {
            User user2 = proper(user);//密码加密
            save(user2);//保存到数据库中
            User user1 = changeUser(user2);//隐藏密码
            return ServerResponse.createBySuccess(user1);//返回登录成功
        }
    }

    /**
     * 修改密码
     *
     * @param id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    public ServerResponse updatePassword(int id, String oldPassword, String newPassword) {

        User u = getById(id);
        if (u == null){
            return ServerResponse.createByErrorMsg("请先登录");
        }
        if (oldPassword == null || oldPassword.equals("")){
           return ServerResponse.createByErrorMsg("密码为空");
        }
        if (newPassword == null || newPassword.equals("")){
            return ServerResponse.createByErrorMsg("新密码不能为空");
        }
        oldPassword = MD5Util.MD5EncodeUtf8(oldPassword);
        newPassword = MD5Util.MD5EncodeUtf8(newPassword);
        if (oldPassword == newPassword) {
            return ServerResponse.createByErrorMsg("修改的新密码不能与原密码一样");
        } else if (u.getPassword().equals(oldPassword)) {
            u.setPassword(newPassword);
            updateById(u);
            User user = getById(id);
            User user1 = changeUser(user);//隐藏密码
            return ServerResponse.createBySuccess(user1);//修改成功
        } else
            return ServerResponse.createByErrorMsg("修改失败");
    }

    /**
     * 修改用户状态为激活
     *
     * @param id 用户ID
     */
    public void updateUserstatus(int id) {
        User user = getById(id);
        user.setStatus(1);
        updateById(user);
    }

    /**
     * 修改用户信息
     *
     * @param user
     */
    public void updateUserinfo(User user) {
        updateById(user);
    }

    /**
     * 密码加密，标记用户状态
     *
     * @param user
     * @return
     */
    public User proper(User user) {
        String password = MD5Util.MD5EncodeUtf8(user.getPassword());//密码加密
        user.setPassword(password);
        user.setStatus(1);  //标记用户状态为未激活
        return user;
    }


    /**
     * @Description: 根据用户ID查询用户payPsd
     * @Param: userId
     * @Param:
     * @return: payPsd
     * @Author: Shawsw
     * @Date: 2019/8/27 11:12
     */
    public String seletePayPsdByUserId(Integer userId) {
        User user = baseMapper.selectOne(new QueryWrapper<User>().eq("userId", userId));
        if (user == null) {
            return null;
        }
        return user.getPayPsd();
    }

    /**
     * @Description:
     * @Param:
     * @Param:
     * @return:
     * @Author: Shawsw
     * @Date: 2019/8/27 13:32
     */
    public User selectUserByUserId(Integer userId) {
        return baseMapper.selectById(userId);
    }

    /**
     * 分页显示User
     *
     * @param current 当前页码
     * @param size    每页大小
     */
    public IPage<User> searchAllUsers(Integer current, Integer size) {
        return baseMapper.selectPage(new Page<>(current, size), new QueryWrapper<User>());
    }
}
